<?php
// 剪刀石头布
namespace plugin\webman\scissors;

use GatewayWorker\Lib\Gateway;
use support\Redis;
use support\Db;
use Workerman\Timer;
use plugin\webman\common\Common;
use plugin\webman\sports\Sports;
use plugin\webman\gateway\Option;
use plugin\webman\gateway\User;
use plugin\webman\scissors\Scissors;
class Event_scissors
{
    public static function onWorkerStart($worker)
    {

        // if($worker->id === 0){
        //     $key = 'timer_scissors';
        //     $timer = Redis::get($key);
        //     var_dump('剪刀石头布定时器',$timer);
        //     if($timer==0){
        //         Timer::add(1, function(){
        //             Sports::timer();   //定时发送各个时间阶段的通知
        //         });
        //         Redis::set($key, 1); // echo('定时器启动');
        //     }
        // }

    }

    public static function onConnect($client_id)
    {

    }

    public static function onWebSocketConnect($client_id, $data)
    {

    }

    public static function onMessage($client_id, $message)
    {
        var_dump("剪刀石头布");
        $data=is_array($message) ? $message : json_decode($message,1);
        $action= $data['action'] ? $data['action'] :'other';
       
        // if($data['action']!='getconfig' || $data['action'] !='login' || $data['action'] !='userreg'){
        //     // 绑定uid 和client_id
        
        // }
     

        switch ($action) {
            case 'checkurl':
                $checkurl = Sports::checkurl($client_id,$data);
                Gateway::sendToClient($client_id,Common::returndata('returncheckurl',$checkurl));
                break;
            case 'getconfig':
                $config = Option::getconfig();
                Gateway::sendToClient($client_id,Common::returndata('returnconfig',$config));
                break;
             //注册 
            case 'userreg':
                $reg = User::userreg($data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnuserreg',$reg));
                break;
            // 登录
            case 'login':
                
                $res = User::userlogin($data['data']);
                if($res['code']==1){
                    $key = 'binduid_'.$client_id;  //绑定uid写入redis
                    $useredis = Redis::set($key,$res['info']['userinfo']['id']);
                    Gateway::bindUid($client_id,$res['info']['userinfo']['id']);
                }
                
                Gateway::sendToClient($client_id,Common::returndata('returnlogin',$res));
                break;
            case 'getchargerules':
                $res = User::getchargerules($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('chargerules',$res));
                break;
            case 'charge':
                // 充值
                $res = User::charge($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('payinfo',$res));
                break;
            case 'getdrawrule':
                // 获取提现规则
                $res = User::drawrules($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('drawrules',$res));
                break;
            case 'withdraw':
                // 提现
                $res = User::withdraw($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('withdrawinfo',$res));
                break;  
            case 'enterroom':
                Gateway::bindUid($client_id, $data['data']['uid']);
                $res = Sports::enterroom($client_id,$data['data']);             
                Gateway::sendToClient($client_id,Common::returndata('returnenterroom',$res));
                break;  
            case 'touzhu':
                $res = Sports::touzhu($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returntouzhu',$res));
                break;
            case 'createroom':
                $res = Scissors::createroom($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returncreateroom',$res));
                break;
            case 'closeroom':
                $res = Scissors::closeroom($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returncloseroom',$res));
                break;
            case 'joinroom':
                $res = Scissors::joinroom($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returncjoinroom',$res));
                break;
            case 'setsocre':
                $res = Scissors::setsocre($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnsetsocre',$res));
                break;
            case 'ready':
                $res = Scissors::ready($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnready',$res));
                break;
            case 'fenrun':
                $res = Scissors::fenrun($client_id,$data['data']);
                // Gateway::sendToClient($client_id,Common::returndata('returnfenrun',$res));
                break;               
        }
    }

    public static function onClose($client_id)
    {
        // 断线走关闭/退出房间接口
        // $uid = Gateway::getUidByClientId($client_id);
        // Scissors::Oncloseroom($client_id,$uid);

    }

}
